﻿using System;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Runtime.Serialization;
using WindowsPhoneEssentials.Expressions;

namespace WindowsPhoneEssentials.ComponentModel
{
    /// <summary>
    /// Helper class if you quickly and easially want to implement INotifyPropertyChanged
    /// </summary>
    [DataContract]
    public class NotifyPropertyChanged : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        protected void OnPropertyChanged<T>(Expression<Func<T>> propertySelector)
        {
            OnPropertyChanged(PropertyResolver.NameFor(propertySelector));
        }
    }
}